package com.dongqb.common;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Calendar;

/**
 * 字符串工具类
 * 
 * @author zhangchaofeng
 * @version 1.0
 * @date Sep 29, 2011
 */
public class MyUtil {
	public static String getStringValue(Object obj) {
		if (obj == null) {
			return "";
		}
		return obj.toString();
	}

	public static int getStringLength(String str) {
		if (str == null) {
			return 0;
		}
		return str.getBytes().length;
	}

	public static boolean hasValue(Object o) {
		if (o == null || o.toString().trim().equals("")) {
			return false;
		}
		return true;
	}

	public static boolean isNumber(String str) {
		String reg = "^[-|+]?\\d*([.]\\d+)?$";
		return str.matches(reg);
	}

	public static String getPath() {
		URL url = MyUtil.class.getProtectionDomain().getCodeSource().getLocation();
		String filePath = null;
		try {
			filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (filePath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"
			// 截取路径中的jar包名
			filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
		}else {
			filePath = filePath.substring(0, filePath.lastIndexOf("/bin") + 1);
		}
		File file = new File(filePath);
		filePath = file.getAbsolutePath();// 得到windows下的正确路径
		return filePath;
	}
	/**
	  * 获取系统时间
	  * @return String(yyyyMMddHHmmss)
	  */
    public static String gettime(){
  	  Calendar now=Calendar.getInstance();
  	  String time=now.get(Calendar.YEAR)+""+(now.get(Calendar.MONTH)+1)+""+now.get(Calendar.DAY_OF_MONTH)+""+now.get(Calendar.HOUR_OF_DAY)+""+now.get(Calendar.MINUTE)+""+now.get(Calendar.SECOND);
  	  return time;
    }
    
	public static void main(String[] args) {
		String str = "0123";
		System.out.println(isNumber(str));
	}
}
